﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using SunTzu.Core.Data;
using Odyssey.Web.Models;

namespace Odyssey.Web.Areas.Admin.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            if (!SessionManager.IsLogin())
            {
                return Redirect("/admin/user/login");
            }
            return View();
        }

        protected OdysseyDataContext db
        {
            get { return DataContextManager.GetContext(); }
        }

        protected IQueryable<T> GetTable<T>() where T : class, IEntity, new()
        {
            return db.GetTable<T>();
        }


        public ActionResult Left()
        {
            //System.Diagnostics.Debugger.Launch();
            if (SessionManager.UserPermits == null || SessionManager.UserPermits.Count == 0)
            {
                ViewData.Add("Module", GetTable<tb_module>().Where(item => item.ParentModule == "0000").OrderBy(item => item.sortorder).ToList());
                ViewData.Add("SubModule", GetTable<tb_module>().Where(item => item.ParentModule != "0000").ToList());
            }
            else
            {
                List<tb_role> allRoles = GetTable<tb_role>().ToList();
                List<tb_permit> allPermits = GetTable<tb_permit>().ToList();
                List<tb_role_permit> allRolePermeits = GetTable<tb_role_permit>().ToList();
                List<tb_module> allModules = GetTable<tb_module>().Where(item => item.ParentModule == "0000").ToList();
                List<tb_module> allSubModules = GetTable<tb_module>().Where(item => item.ParentModule != "0000").ToList();

                List<tb_role_permit> userRolePermits = new List<tb_role_permit>();
                foreach (var userPermit in SessionManager.UserPermits)
                {
                    if (!string.IsNullOrEmpty(userPermit.RoleCode))
                    {
                        string[] roleCodes = userPermit.RoleCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach(var roleCode in roleCodes)
                        {
                            foreach (var rolePermit in allRolePermeits)
                            {

                                if (roleCode.Equals(rolePermit.RoleCode, StringComparison.OrdinalIgnoreCase))
                                {
                                    if (userRolePermits.Contains(rolePermit))
                                    {
                                        continue;
                                    }
                                    userRolePermits.Add(rolePermit);
                                }
                            }
                        }

                    }
                }

                List<tb_permit> userPermits = new List<tb_permit>();
                foreach (var rolePermit in userRolePermits)
                {
                    if (!string.IsNullOrEmpty(rolePermit.PermitCode))
                    {
                        string[] userPermitCodes = rolePermit.PermitCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string permitCode in userPermitCodes)
                        {
                            foreach (var permit in allPermits)
                            {
                                if (permitCode.Equals(permit.permitcode, StringComparison.OrdinalIgnoreCase))
                                {
                                    if (!userPermits.Contains(permit))
                                    {
                                        userPermits.Add(permit);
                                    }
                                }
                            }
                        }
                    }
                }

                List<tb_module> userModules = new List<tb_module>();
                List<tb_module> userSubModules = new List<tb_module>();
                foreach (tb_permit permit in userPermits)
                {
                    foreach (tb_module module in allModules)
                    {
                        if (permit.ModuleCode.Equals(module.ModuleCode, StringComparison.OrdinalIgnoreCase))
                        {
                            if (!userModules.Contains(module))
                            {
                                userModules.Add(module);
                            }
                        }
                    }

                    foreach (tb_module module in allSubModules)
                    {
                        if (permit.ModuleCode.Equals(module.ModuleCode, StringComparison.OrdinalIgnoreCase))
                        {
                            if (!userSubModules.Contains(module))
                            {
                                userSubModules.Add(module);
                            }
                        }
                    }
                }

                //List<tb_role> AllRoles = GetTable<tb_role>().ToList();
                //List<tb_role> AllRoles = GetTable<tb_role>().ToList();
                ViewData.Add("Module",userModules);
                ViewData.Add("SubModule", userSubModules);
            }
            return View();
        }
        public ActionResult Admintitle()
        {
            return View();
        }
        public ActionResult Right()
        {
            return View();
        }


        public ActionResult Admin_Top()
        {
            return View();
        }

    }
}
